<?php
class Commqpassuser extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'commqpassuser';
        $this->_list_where = array();
        $this->_action_list = array();
        $this->_controller = 'admin/commqpassuser/';//admin/user/
        parent::__construct();
    }

    public function showList($qid,$page=1)
    {
        $where = array();
        $get = $this->input->get();
        if (!empty($get)){
            $applystart = !empty($get['applystart'])?strtotime(trim($get['applystart'])):'';
            $applyend = !empty($get['applyend'])?strtotime(trim($get['applyend'])):'';
            $prostart = !empty($get['prostart'])?strtotime(trim($get['prostart'])):'';
            $proend = !empty($get['proend'])?strtotime(trim($get['proend'])):'';
            $tel = !empty($get['ftelnum'])?trim($get['ftelnum']):'';
            $user = !empty($get['fusername'])?trim($get['fusername']):'';
            $status = !empty($get['fstatus'])?(int)$get['fstatus']:'';
            if (!empty($applystart)) $where['qu.created >'] = $applystart;
            if (!empty($applyend)) $where['qu.created <'] = $applyend;
            if (!empty($prostart)) $where['qu.changed >'] = $prostart;
            if (!empty($proend)) $where['qu.changed <'] = $proend;
            if (!empty($user)) $where['u.userName LIKE']= '%'.$user.'%';
            if (!empty($tel)) $where['u.telNum LIKE']= '%'.$tel.'%';
            if (!empty($status)) $where['qu.status =']= $status;
            $filter = $this->load->view('admin/form/filter/qpassfilter',array('controller'=>site_url($this->_controller),'qid'=>$qid,
                'prostart'=>$get['prostart'],'proend'=>$get['proend'], 'applystart'=>$get['applystart'],'applyend'=>$get['applyend'],
                'fusername'=>$user,'ftelnum'=>$tel,'fstatus'=>$status),true);
        }else{
            $filter = $this->load->view('admin/form/filter/qpassfilter',array('controller'=>site_url($this->_controller),'qid'=>$qid),true);
        }
        $where['qu.qid']=$qid;
        $this->_list_where = $where;
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/'.$qid.'/');

        $res = $this->getList($offset,$limit);

        //有返回按钮
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/commqpass/')),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'backbtn'=>$backbtn,'filter'=>$filter);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }
    //重写获取list数据
    public function getList($offset = 0,$limit = 0)
    {
        $this->db->select('qu.id,qu.qid,u.userName,u.userIcon,u.telNum,qu.created,qu.changed,qu.status')->from('comm_qpass_user as qu')
            ->join('mod_user as u','u.uid = qu.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $this->db->order_by('qu.created','DESC');
        if (!empty($limit)) $this->db->limit($limit,$offset);
        $data=$this->db->get()->result_array();

        $res = array('headers'=>array(),'list'=>array());
        foreach($data as $row){
            $rowdata = array();
            $rowdata['userIcon']=preg_match("/http:\/\//i",$row['userIcon'])>0?$row['userIcon']:$this->_resource_url.$row['userIcon'];
            $rowdata['userIcon']='<img src="'.$rowdata['userIcon'].'" style="height:30px;">';
            $rowdata['userName']=$row['userName'];
            $rowdata['telNum']=$row['telNum'];
            $rowdata['created']=date('Y-m-d H:i:s',$row['created']);
            $rowdata['changed']=$row['changed']>0?date('Y-m-d H:i:s',$row['changed']):'';
            switch ($row['status']){
                case 1:
                    $rowdata['status']='等待审核';
                    break;
                case 2:
                    $rowdata['status']='审核通过';
                    break;
                case 3:
                    $rowdata['status']='审核未通过';
                    break;
                default:
                    $rowdata['status']='';
            }
            $rowdata['actionlist']= '<a class="other-btn action-btn" href="'.site_url($this->_controller).
                'update/'.$row['id'].'/'.$row['qid'].'">审核</a>';
            $res['list'][]=$rowdata;
        }
        $res['headers']=array('头像','用户名','电话','申请时间','处理时间','状态','操作');
        return $res;
    }
    public function getPagination($uri = '')
    {
        $this->db->from('comm_qpass_user as qu')->join('mod_user as u','u.uid = qu.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'1?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function update($id = 0,$qid = 0)
    {
        $this->_external_id=$qid;
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form,'param'=>$qid),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['status']=(int)$post['status'];
            $post['changed']=time();
            $this->_update_data = $post;
            parent::update($id);
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create(){echo 'invalid request';}

    public function aftercreate($pkeyval){echo 'invalid request';}

    public function delete($id = 0,$externalid = 0){echo 'invalid request';}

    public function afterdelete($pkey,$pkeyval){echo 'invalid request';}

    public function allpass($qid)
    {
        $data = array('status'=>2,'changed'=>time());
        $this->db->update('comm_qpass_user',$data,array('qid'=>$qid,'status'=>1));//将等待审核的全部通过
        redirect(site_url($this->_controller).'showList/'.$qid.'/1');;
    }
}